package br.edu.utfpr.backuptools.BackupTools.model.util.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;

import br.edu.utfpr.backuptools.BackupTools.controller.ControladorConfiguracaoImpl;
import br.edu.utfpr.backuptools.BackupTools.controller.interfaces.ControladorConfiguracao;
import br.edu.utfpr.backuptools.BackupTools.controller.validator.ConfiguracaoValidator;
import br.edu.utfpr.backuptools.BackupTools.model.dao.DaoConfiguracaoImpl;
import br.edu.utfpr.backuptools.BackupTools.model.dao.interfaces.DaoConfiguracao;
import br.edu.utfpr.backuptools.BackupTools.model.pojo.Configuracao;

public class GeraBanco
{
	public static void main(String[] args)
	{
		Configuration configuration = new HibernateConfiguration().getConfiguration();
		configuration.configure();
		
		boolean atualizar = false;
		if(atualizar)
		{
			SchemaUpdate su = new SchemaUpdate(configuration);
			su.execute(true, true);
		}
		else
		{
			SchemaExport se = new SchemaExport(configuration);
			se.create(true, true);
			try
			{
				criaConfiguracaoDefault();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			} // só cria o admin quando cria o banco
		}
	}
	
	private static void criaConfiguracaoDefault() throws Exception
	{
		HibernateUtil.getInstance().currentSession();
		HibernateUtil.getInstance().beginTransaction();
		
		Configuracao c = new Configuracao();
		c.setHora(0);
		c.setMinutos(00);
		
		DaoConfiguracao dao = new DaoConfiguracaoImpl();
		ControladorConfiguracao controlador = new ControladorConfiguracaoImpl(dao,new ConfiguracaoValidator());
		controlador.save(c);
		
		HibernateUtil.getInstance().commitTransaction();
		System.exit(0);
	}
}
